<template>
    <div>

        <div class="title">
            <div class="desc">职业资格证书</div>
            <div>
                <el-button type="primary" @click="handleAdd" :disabled="flag == 3">添加</el-button>
            </div>
        </div>

        <el-table :data="membersList" class="table">

            <el-table-column label="证书名称" >
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.certificateName"  maxlength="20" show-word-limit   placeholder="请输入证书名称"></el-input>
                    <span v-else>{{ scope.row.certificateName }}</span>
                </template>
            </el-table-column>


            <el-table-column label="签发日期">
                <template #default="scope">
                    <el-date-picker  v-if="activeIndex == scope.$index" v-model="scope.row.issueDate"   type="data"
                    value-format="YYYY-MM-DD" placeholder="开始时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.issueDate }}</span>
                </template>
            </el-table-column>

            <el-table-column align="right" width="250">
                <template #header>
                    <span>操作</span>
                </template>
                <template #default="scope">
                    <div v-if="activeIndex == scope.$index">

                        <el-button type="success" @click="handleSave">保存</el-button>
                      <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                                     cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                        <template #reference>
                          <el-button type="danger" :disabled="flag == 3">删除</el-button>
                        </template>
                      </el-popconfirm>
                    </div>
                    <div v-else>
                        <el-button type="primary" @click="handleEdit(scope.$index)" :disabled="flag == 3">编辑</el-button>
                        <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                            cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                            <template #reference>
                                <el-button type="danger" :disabled="flag == 3">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>



<script  lang="ts">

import { ref, watch } from 'vue';
import {tips} from "@/views/science/components/tips/tips";

export default {


    components: {

    },
    emits: {
        dataChange: null,
        paramsChange: null,
    },
    props: {
        members: {
            type: Array,
            required: true,
        },
      // falg  1:新增  2:编辑  3:详情
      flag: {
        type: String,
        default: 1,
      },
    },
    setup(props, { emit }) {

        let activeIndex = ref(-1);


        const membersList = ref(props.members) as any

        watch(props, (newValue, oldValue) => {
            membersList.value = newValue.members
        });


        const handleAdd = () => {

            let item = {
                issueDate:'',
              certificateName:'',
            };
            let flag = true
            membersList.value.every(item=>{
              console.log(item,'item');
              for(let key in item){
                if(item[key]===''){
                  flag =  false
                  return false
                }
              }
              return true
            })
            if(!flag){
              tips("请填写完整数据", "warning");

              return
            }
            membersList.value.push(item);

            activeIndex.value =  membersList.value.length - 1;

        }

        const handleEdit = (index: number) => {

            activeIndex.value = index;
        };

        const handleSave = () => {

          let panduan = 0 ;
          membersList.value.forEach(item => {
            for (let i = 0; i < membersList.value.length; i++) {
              console.log(323232,item)
              if(item.certificateName == null || item.certificateName == ''){
                panduan = 1;
              }
              if(item.issueDate == null || item.issueDate == ''){
                panduan = 2;
              }

            }
          });
          if(panduan == 1){
            tips("数据中证书名称不能为空", "warning");
            // event.stopPropagation();
            return false;
          }else if(panduan == 2){
            tips("数据中签发日期不能为空", "warning");
            return false;
          }


          activeIndex.value = -1;

            emit('paramsChange',  membersList.value)

        };

        const handleDelete = (index: number) => {
            membersList.value.splice(index, 1);

            emit('paramsChange',  membersList.value)
        };

        return {

            membersList,
            activeIndex,
            handleAdd,
            handleEdit,
            handleSave,
            handleDelete
        }


    },
}
</script>












<style scoped lang="scss">
.title {
    display: flex;
    justify-content: space-between;

    .desc {
        font-weight: bolder;
    }
}

.table {
    margin: 15px 0;
}
</style>
